package com.dimong.loan.sdk.ui.frag;

import android.os.Bundle;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.dimong.loan.sdk.app.BaseApplication;
import com.google.gson.Gson;

public abstract class BaseFragment extends Fragment {

    protected final String TAG = getClass().getSimpleName();
    protected Gson gson = new Gson();
    protected View mContentView;

    /**
     * 初始化页面之前的操作，用于不同页面类型的差异化操作，止于模板层，具体页面无需实现
     */
    public abstract void beforeInitView();

    /**
     * 定义页面控件
     */
    public abstract void initView();

    /**
     * 设置页面控件事件和状态
     */
    public abstract void setViewStatus();

    /**
     * 获取布局资源id
     *
     * @return
     */
    public abstract int getViewResourceId();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return mContentView = inflater.inflate(getViewResourceId(),
            container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        beforeInitView();
        initView();
        setViewStatus();
    }

    /**
     * 获取视图
     *
     * @param id
     * @return
     */
    protected View findViewById(int id) {
        return mContentView.findViewById(id);
    }

    /**
     * Fragment是否消费处理了返回键
     *
     * @return true:fragment处理后拦截; false:fragment处理后,下一层继续处理
     */
    public boolean onBackPressedFrag() {
        return false;
    }

    protected void showToast(String msg) {
        Toast.makeText(BaseApplication.getApplication(), msg, Toast.LENGTH_SHORT).show();
    }

    protected void showToast(int msgId) {
        showToast(BaseApplication.getApplication().getResources().getString(msgId));
    }


    public void handleFragmentMessage(Message msg) {
    }
}
